/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaEntity;
import han.bifang.common.entity.SoaHttpBinding;
import han.bifang.common.entity.SoaJmsBinding;
import han.bifang.common.entity.SoaJmsBindingWrapper;
import han.bifang.common.entity.SoaMqJms;
import han.bifang.common.entity.SoaNormalJms;
import han.bifang.common.entity.SoaSoapBinding;
import han.bifang.common.entity.SoaWebServicesBinding;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class ServiceBindingDao {
	
	public static String createBinding(Object v)throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			tx.commit();
			if(v instanceof SoaWebServicesBinding){
				DaoManager.getDao().save(v);
				
				return ((SoaWebServicesBinding)v).getId();
			}else if(v instanceof SoaHttpBinding){
				DaoManager.getDao().save(v);
				
				return ((SoaHttpBinding)v).getId();
			}else if(v instanceof SoaSoapBinding){
				DaoManager.getDao().save(v);
				
				return ((SoaSoapBinding)v).getId();
			}else if(v instanceof SoaJmsBindingWrapper){
				SoaJmsBindingWrapper w = (SoaJmsBindingWrapper)v;
				DaoManager.getDao().save(w.getInfo());
				SoaEntity t = (SoaEntity)w.getInfo();
				w.getJmsBinding().setInfoId(t.getId());
				DaoManager.getDao().save(w.getJmsBinding());
				return w.getJmsBinding().getId();
			}
			return null;
			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceBindingDao.class);
			logger.error("ServiceBindingDao.createBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createBinding() error", e);
		} finally {
			tx.end();
		}
	}
	
	public static void modifyBinding(Object v)throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			if(v instanceof SoaJmsBindingWrapper){
				SoaJmsBindingWrapper w = (SoaJmsBindingWrapper)v;
				DaoManager.getDao().merge(w.getInfo());
				DaoManager.getDao().merge(w.getJmsBinding());
			}else
				DaoManager.getDao().merge(v);
			tx.commit();
			
			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceBindingDao.class);
			logger.error("ServiceBindingDao.modifyBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"modifyBinding() error", e);
		} finally {
			tx.end();
		}
	}
	
	public static void removeHttpBinding(String id)throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			Object o = DaoManager.getDao().get(SoaHttpBinding.class, id);
			if(o != null)
				DaoManager.getDao().delete(o);
			tx.commit();
			
			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceBindingDao.class);
			logger.error("ServiceBindingDao.removeHttpBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeHttpBinding() error", e);
		} finally {
			tx.end();
		}
	}
	
	public static void removeSoapBinding(String id)throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			Object o = DaoManager.getDao().get(SoaSoapBinding.class, id);
			if(o != null)
				DaoManager.getDao().delete(o);
			tx.commit();
			
			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceBindingDao.class);
			logger.error("ServiceBindingDao.removeSoapBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeSoapBinding() error", e);
		} finally {
			tx.end();
		}
	}
	
	public static void removeJmsBinding(String id)throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaJmsBinding o = (SoaJmsBinding)DaoManager.getDao().get(SoaJmsBinding.class, id);
			if(o != null){
				if(o.getServer().equals("mq")){
					Object info = DaoManager.getDao().get(SoaMqJms.class, o.getInfoId());
					DaoManager.getDao().delete(info);
					
				}else{
					Object info = DaoManager.getDao().get(SoaNormalJms.class, o.getInfoId());
					DaoManager.getDao().delete(info);
					
				}
				DaoManager.getDao().delete(o);
			
			}
			tx.commit();
			
			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceBindingDao.class);
			logger.error("ServiceBindingDao.removeJmsBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeJmsBinding() error", e);
		} finally {
			tx.end();
		}
	}
	
	public static void removeWebServicesBinding(String id)throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			Object o = DaoManager.getDao().get(SoaWebServicesBinding.class, id);
			if(o != null)
				DaoManager.getDao().delete(o);
			tx.commit();
			
			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceBindingDao.class);
			logger.error("ServiceBindingDao.removeWebServicesBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeWebServicesBinding() error", e);
		} finally {
			tx.end();
		}
	}
	
	public static SoaHttpBinding getHttpBinding(String id)throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaHttpBinding o = (SoaHttpBinding)DaoManager.getDao().get(SoaHttpBinding.class, id);
			
			tx.commit();
			return o;
			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceBindingDao.class);
			logger.error("ServiceBindingDao.getHttpBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getHttpBinding() error", e);
		} finally {
			tx.end();
		}
	}
	
	public static SoaSoapBinding getSoapBinding(String id)throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaSoapBinding o = (SoaSoapBinding)DaoManager.getDao().get(SoaSoapBinding.class, id);
			
			tx.commit();
			return o;
			

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceBindingDao.class);
			logger.error("ServiceBindingDao.getSoapBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getSoapBinding() error", e);
		} finally {
			tx.end();
		}
	}
	
	public static SoaJmsBindingWrapper getJmsBinding(String id)throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaJmsBindingWrapper w = new SoaJmsBindingWrapper();
			SoaJmsBinding o = (SoaJmsBinding)DaoManager.getDao().get(SoaJmsBinding.class, id);
			w.setJmsBinding(o);
			if(o.getServer().equals("mq")){
				SoaMqJms j = (SoaMqJms)DaoManager.getDao().get(SoaMqJms.class, o.getInfoId());
				w.setInfo(j);
			}else{
				SoaNormalJms j = (SoaNormalJms)DaoManager.getDao().get(SoaNormalJms.class, o.getInfoId());
				w.setInfo(j);
			}
			tx.commit();
			
			return w;

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceBindingDao.class);
			logger.error("ServiceBindingDao.getJmsBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getJmsBinding() error", e);
		} finally {
			tx.end();
		}
	}
	
	public static SoaWebServicesBinding getWebServicesBinding(String id)throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaWebServicesBinding o = (SoaWebServicesBinding)DaoManager.getDao().get(SoaWebServicesBinding.class, id);
			
			tx.commit();
			
			return o;

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceBindingDao.class);
			logger.error("ServiceBindingDao.removeWebServicesBinding()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeWebServicesBinding() error", e);
		} finally {
			tx.end();
		}
	}



}
